package servlet.visitor;

import bean.dto.VisitorQueryPage;
import bean.pojo.Visitor;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import commons.xlhUtils.MyFormatUtils;
import service.VisitorService;
import service.impl.VisitorServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * servlet.visitor
 * User: 坚持
 * Date: 2024/12/19 10:10
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
@WebServlet(name = "VisitorServlet",value = "/visitor/*")
public class VisitorServlet extends BaseServlet {

    /**
     * 添加访客记录
     * @param request
     * @return
     */
    public R add(HttpServletRequest request){
        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data = bufferedReader.readLine();
            Visitor visitor = JSONObject.parseObject(req_data,Visitor.class);
            VisitorService visitorService = new VisitorServiceImpl();
            Boolean add = visitorService.add(visitor);
            return add?R.ok():R.error("添加失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    /**
     * 删除访客记录
     * @param request
     * @return
     */
    public R deleteById(HttpServletRequest request){
        Integer visitorId = NumberFormat.stringToInteger(request.getParameter("visitorId"));
        VisitorService visitorService = new VisitorServiceImpl();
        Boolean delete = visitorService.deleteById(visitorId);
        return delete?R.ok():R.error("删除失败");
    }

    /**
     * 修改访客记录
     * @param request
     * @return
     */
    public R update(HttpServletRequest request){

        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data = bufferedReader.readLine();
            Visitor visitor = JSONObject.parseObject(req_data,Visitor.class);
            VisitorService visitorService = new VisitorServiceImpl();
            Boolean update = visitorService.update(visitor);
            return update?R.ok():R.error("修改失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    /**
     * 根据姓名、电话、时间模糊查询访客记录
     * @param request
     * @return
     */
    public R findBy(HttpServletRequest request){
        Integer page = NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit = NumberFormat.stringToInteger(request.getParameter("limit"));
        String visitorName = request.getParameter("visitorName");
        String visitorPhone = request.getParameter("visitorPhone");
        String visitTime = request.getParameter("visitTime");
        VisitorQueryPage visitorQueryPage=new VisitorQueryPage();
        visitorQueryPage.setPage(page);
        visitorQueryPage.setLimit(limit);
        visitorQueryPage.setVisitorName(visitorName);
        visitorQueryPage.setVisitorPhone(visitorPhone);
        visitorQueryPage.setVisitTime(visitTime);

        VisitorService visitorService = new VisitorServiceImpl();
        Long total = visitorService.findByTotal(visitorQueryPage);

        List<Visitor> list=null;
        if(total>0) {
            //当前页码的数据
            list = visitorService.visitorFindBy(visitorQueryPage);
        }
        return R.ok().addData("total",total).addData("list",list);
    }
}
